<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区" prop="communityId">
        <el-select v-model="queryParams.communityId"
         class="width100"
         placeholder="所属小区" clearable>
          <el-option
           v-for="item in communityArr"
           :key="item.id"
           :label="item.communityName"
           :value="item.id"
          />
        </el-select>
      </el-form-item>
	  <el-form-item label="楼宇" prop="floorName">
	    <el-select v-model="queryParams.floorName"
	     class="width100"
	     placeholder="楼宇" clearable>
	      <el-option
	       v-for="item in ListFloorArr"
	       :key="item.id"
	       :label="item.floorName"
	       :value="item.id"
	      />
	    </el-select>
	  </el-form-item>
	  <el-form-item label="单元号" prop="units">
	    <el-input
	      v-model="queryParams.units"
	      placeholder="请输入单元号"
	      clearable
	    />
	  </el-form-item>
	  <el-form-item label="楼层" prop="layers">
	    <el-input
	      v-model="queryParams.layers"
	      placeholder="请输入楼层"
	      clearable
	    />
	  </el-form-item>
	  <el-form-item label="单元数" prop="units">
	    <el-input
	      v-model="queryParams.units"
	      placeholder="请输入单元数"
	      clearable
	    />
	  </el-form-item>
	  <el-form-item label="房屋号" prop="houseNum">
	    <el-input
	      v-model="queryParams.houseNum"
	      placeholder="请输入房屋号"
	      clearable
	    />
	  </el-form-item>
	  <el-form-item label="房屋状态" prop="roomStatus">
	    <el-select v-model="queryParams.roomStatus"
	     class="width100"
	     placeholder="房屋状态" clearable>
	      <el-option
	       v-for="item in houseType"
	       :key="item.type"
	       :label="item.name"
	       :value="item.type"
	      />
	    </el-select>
	  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" 
		
	> 
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="所属小区" align="center" prop="communityName" />
      <el-table-column label="楼宇名称" align="center" prop="floorName" />
      <el-table-column label="单元数" align="center" prop="units" />
      <el-table-column label="楼层数" align="center" prop="floor" />
      <el-table-column label="房屋数" align="center" prop="roomNo" />
      <el-table-column label="房型" align="center" prop="buildingDesignName" />
      <el-table-column label="建筑面积" align="center" prop="buildArea" />
      <el-table-column label="状态" align="center">
		  <template slot-scope="scope">
			  {{ scope.row.roomStatusCn }}
			  <el-tooltip class="item" effect="dark" :content="scope.row.reviewComment" placement="bottom-start">
				<i class="el-icon-info" v-if='scope.row.roomStatus == 9'></i>
			  </el-tooltip>
		  </template>
	  </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改</el-button>
		  <el-button
		    size="mini"
			v-if='scope.row.roomStatus == 3 || scope.row.roomStatus == 9'
		    type="text"
		    icon="el-icon-delete"
		    @click="handleEditStatus(scope.row)"
		    v-hasPermi="['system:post:remove']"
		  >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属小区" prop="communityId">
          <el-select v-model="form.communityId"
           class="width100"
          			placeholder="所属小区" clearable>
            <el-option
              v-for="item in communityArr"
              :key="item.id"
              :label="item.communityName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="楼宇名称" prop="floorId">
          <el-select v-model="form.floorId"
           class="width100"
           placeholder="楼宇" clearable>
            <el-option
             v-for="item in ListFloorArr"
             :key="item.id"
             :label="item.floorName"
             :value="item.id"
            />
          </el-select>
        </el-form-item>
		<el-form-item label="房型" prop="buildingDesignId">
		  <el-select v-model="form.buildingDesignId"
		   class="width100"
		   placeholder="房型" clearable>
		    <el-option
		     v-for="item in HomeTypeList"
		     :key="item.id"
		     :label="item.name"
		     :value="item.id"
		    />
		  </el-select>
		</el-form-item>
		<el-form-item label="单元号" prop="units">
		  <el-input placeholder="请输入单元数" type='number' v-model="form.units">
		     <template slot="append">单元</template>
		  </el-input>
		</el-form-item>
		<el-form-item label="楼层数" prop="floor">
		  <el-input placeholder="请输入楼层数" v-model="form.floor">
		  </el-input>
		</el-form-item>
		<el-form-item label="房间数" prop="roomNo">
		  <el-input placeholder="请输入房间数" type='number' v-model="form.roomNo">
		     <template slot="append">号</template>
		  </el-input>
		</el-form-item>
		<el-form-item label="建筑面积" prop="buildArea">
			<el-input placeholder="请输入建筑面积" type='number' v-model="form.buildArea">
			    <template slot="append">㎡</template>
			  </el-input>
		</el-form-item>
		<el-form-item label="使用面积" prop="usableArea">
			<el-input placeholder="请输入使用面积" type='number' v-model="form.usableArea">
			   <template slot="append">㎡</template>
			</el-input>
		</el-form-item>
		<el-form-item label="公摊面积" prop="publicShareArea">
			<el-input placeholder="请输入使用面积" type='number' v-model="form.publicShareArea">
			   <template slot="append">㎡</template>
			</el-input>
		</el-form-item>
		<el-form-item label="房屋状态" prop="roomStatus">
		 <el-select v-model="form.roomStatus"
			 class="width100"
			 placeholder="房屋状态" clearable>
			  <el-option
			   v-for="item in houseType"
			   :key="item.type"
			   :label="item.name"
			   :value="item.type+''"
			  />
			</el-select>
		</el-form-item>
        <el-form-item label="备注" prop="notes">
			<el-input v-model="form.notes" placeholder="备注" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
	<!--挂租审核-->
	<!-- 添加或修改岗位对话框 -->
	<el-dialog title="挂租审核" :visible.sync="openStatus" width="500px" append-to-body>
	  <el-form ref="formStaus" :model="formStaus" :rules="rulesStatus" label-width="80px">
	    <el-form-item label="托管周期" prop="custodyCycle">
	      <el-date-picker
	        v-model="formStaus.custodyCycle"
	        value-format="yyyy-MM-dd"
	        type="daterange"
	        range-separator="-"
	        start-placeholder="开始日期"
	        end-placeholder="结束日期"
	      ></el-date-picker>
	    </el-form-item>
		<el-form-item label="审核类型" prop="roomStatus">
		  <el-radio-group v-model="formStaus.roomStatus">
		    <el-radio  :label="2">审核通过</el-radio>
		    <el-radio  :label="9">审核拒绝</el-radio>
		  </el-radio-group>
		</el-form-item>
	    <el-form-item label="备注" prop="notes">
			<el-input v-model="form.notes" placeholder="备注" type="textarea" />
	    </el-form-item>
	  </el-form>
	  <div slot="footer" class="dialog-footer">
	    <el-button type="primary" @click="submitFormStaus">确 定</el-button>
	    <el-button @click="cancel">取 消</el-button>
	  </div>
	</el-dialog>
  </div>
</template>
<script>
import { listRoom, delRoom, insertRoom, editRoom, Examine } from "@/api/house/layoutmanagement";
export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
	  //审核弹窗
	  openStatus:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
	  //所属小区
	  communityArr:[],
	  //所有楼宇
	  ListFloorArr:[],
	  //所有房型
	  HomeTypeList:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
		roomStatus: undefined,
        communityId: undefined,
        floorName: undefined,
      },
      // 表单参数
      form: {
		  roomStatus:2
	  },
	  formStaus: { roomStatus:2 },
	  rulesStatus:{
		  custodyCycle: [
		    { required: true, message: "托管周期不能为空", trigger: "blur" }
		  ],
	  },
      // 表单校验
      rules: {
        communityId: [
          { required: true, message: "所属小区不能为空", trigger: "blur" }
        ],
        floorId: [
          { required: true, message: "楼宇名称不能为空", trigger: "blur" }
        ],
        units: [
          { required: true, message: "单元号不能为空", trigger: "blur" }
        ],
        floor: [
          { required: true, message: "楼层数不能为空", trigger: "blur" }
        ],
        roomNo: [
          { required: true, message: "房号不能为空", trigger: "blur" }
        ],
		buildArea: [
		  { required: true, message: "建筑面积不能为空", trigger: "blur" }
		],
		buildingDesignId: [
		  { required: true, message: "房型不能为空", trigger: "blur" }
		],
		usableArea: [
		  { required: true, message: "使用面积不能为空", trigger: "blur" }
		],
		publicShareArea: [
		  { required: true, message: "公摊面积不能为空", trigger: "blur" }
		],
		roomStatus:[
          { required: true, message: "房屋状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
	this.getAllXiaoQu();
	this.getListFloorArr();
	this.getTypeList()
  },
  computed: {
	  houseType: {
	    get() {
	      return this.$store.state.common.houseType
	    },
	  },
  },
  methods: {
    //获取所有小区
    getAllXiaoQu(){
		this.$store.dispatch('AllCommunityList').then(res => {
			 this.communityArr = res
			 this.getList();
		})
    },
	//获取所有房型
	getTypeList(){
		this.$store.dispatch('AllHomeTypeList').then(res => {
			 this.HomeTypeList = res
		})
	},
	//获取所有楼宇
	getListFloorArr(){
		this.$store.dispatch('AllFloorList').then(res => {
			 this.ListFloorArr = res
		})
	},
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listRoom(this.queryParams).then(response => {
        this.postList = response.rows.map(i=>{
        	i.communityName = i.communityId ? this.communityArr.filter(v=>v.id == i.communityId)[0].communityName : '-';
			i.roomStatusCn = i.roomStatus ? this.houseType.filter(v=>v.type == i.roomStatus)[0].name : '-';
        	return i
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
	  this.openStatus = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10
      };
	  this.form = {}
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
	/** 审核状态 */
	handleEditStatus(res){
		const postId = res.id
		this.formStaus.roomId = postId
		this.openStatus = true;
	},
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房屋";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const postId = row.id
      this.form = row;
      this.open = true;
      this.title = "修改房屋";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editRoom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertRoom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
	/** 审核提交按钮 */
    submitFormStaus: function() {
      this.$refs["formStaus"].validate(valid => {
        if (valid) {
          Examine(this.formStaus).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.openStatus = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */ 
    handleDelete(row) {
      const postIds = {
		  id:row.id
	  };
      this.$modal.confirm('是否确认删除该房屋？').then(function() {
        return delRoom(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
